Let’s define some vectors which can be used for demonstrations:
manyNumbers <- sample( 1:1000, 20 )
manyNumbers
[1] 423 138 387 626 24 475 840 74 366 201 434 428 115 170 988 111 470 724 66 73
manyNumbersWithNA <- sample( c( NA, NA, NA, manyNumbers ) )
manyNumbersWithNA
[1] 840 115 423 366 170 24 475 73 NA 111 201 988 428 724 NA 470 626 NA 138 434 387 66 74
duplicatedNumbers <- sample( 1:5, 10, replace = TRUE )
duplicatedNumbers
[1] 3 4 4 3 2 3 2 2 1 2
letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
mixedLetters <- c( sample( letters, 5 ), sample( LETTERS, 5 ) )
mixedLetters
[1] "b" "p" "a" "f" "q" "Q" "N" "V" "S" "P"
manyNumbersWithNA instead of manyNumbers.all( manyNumbers <= 1000 )
[1] TRUE
all( manyNumbers <= 500 )
[1] FALSE
any( manyNumbers > 1000 )
[1] FALSE
any( manyNumbers > 500 )
[1] TRUE
all( !is.na( manyNumbers ) )
[1] TRUE
any( is.na( manyNumbers ) )
[1] FALSE
Input: logical vector Output: vector of numbers (positions)
which( manyNumbers > 900 )
[1] 15
which( manyNumbersWithNA > 900 )
[1] 12
which( is.na( manyNumbersWithNA ) )
[1] 9 15 18
manyNumbers[ manyNumbers > 900 ] # indexing by logical vector
[1] 988
manyNumbers[ which( manyNumbers > 900 ) ] # indexing by positions
[1] 988
somePositions <- which( manyNumbers > 900 )
manyNumbers[ somePositions ]
[1] 988
"A" %in% LETTERS
[1] TRUE
c( "X", "Y", "Z" ) %in% LETTERS
[1] TRUE TRUE TRUE
all( c( "X", "Y", "Z" ) %in% LETTERS )
[1] TRUE
all( mixedLetters %in% LETTERS )
[1] FALSE
any( mixedLetters %in% LETTERS )
[1] TRUE
mixedLetters[ mixedLetters %in% LETTERS ]
[1] "Q" "N" "V" "S" "P"
mixedLetters[ !( mixedLetters %in% LETTERS ) ]
[1] "b" "p" "a" "f" "q"
manyNumbers %in% 300:600
[1] TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
[20] FALSE
which( manyNumbers %in% 300:600 )
[1] 1 3 6 9 11 12 17
sum( manyNumbers %in% 300:600 )
[1] 7
NAsif_else( manyNumbersWithNA >= 500, "large", "small" )
[1] "large" "small" "small" "small" "small" "small" "small" "small" NA "small" "small" "large" "small" "large"
[15] NA "small" "large" NA "small" "small" "small" "small" "small"
if_else( manyNumbersWithNA >= 500, "large", "small", "UNKNOWN" )
[1] "large" "small" "small" "small" "small" "small" "small" "small" "UNKNOWN" "small" "small"
[12] "large" "small" "large" "UNKNOWN" "small" "large" "UNKNOWN" "small" "small" "small" "small"
[23] "small"
# here integer 0L is needed instead of real 0.0
# manyNumbersWithNA contains integer numbers and the method complains
if_else( manyNumbersWithNA >= 500, manyNumbersWithNA, 0L )
[1] 840 0 0 0 0 0 0 0 NA 0 0 988 0 724 NA 0 626 NA 0 0 0 0 0
unique( duplicatedNumbers )
[1] 3 4 2 1
unique( c( NA, duplicatedNumbers, NA ) )
[1] NA 3 4 2 1
duplicated( duplicatedNumbers )
[1] FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
which.max( manyNumbersWithNA )
[1] 12
manyNumbersWithNA[ which.max( manyNumbersWithNA ) ]
[1] 988
which.min( manyNumbersWithNA )
[1] 6
manyNumbersWithNA[ which.min( manyNumbersWithNA ) ]
[1] 24
range( manyNumbersWithNA, na.rm = TRUE )
[1] 24 988
manyNumbersWithNA
[1] 840 115 423 366 170 24 475 73 NA 111 201 988 428 724 NA 470 626 NA 138 434 387 66 74
sort( manyNumbersWithNA )
[1] 24 66 73 74 111 115 138 170 201 366 387 423 428 434 470 475 626 724 840 988
sort( manyNumbersWithNA, na.last = TRUE )
[1] 24 66 73 74 111 115 138 170 201 366 387 423 428 434 470 475 626 724 840 988 NA NA NA
sort( manyNumbersWithNA, na.last = TRUE, decreasing = TRUE )
[1] 988 840 724 626 475 470 434 428 423 387 366 201 170 138 115 111 74 73 66 24 NA NA NA
manyNumbersWithNA[1:5]
[1] 840 115 423 366 170
order( manyNumbersWithNA[1:5] )
[1] 2 5 4 3 1
rank( manyNumbersWithNA[1:5] )
[1] 5 1 4 3 2
sort( mixedLetters )
[1] "a" "b" "f" "N" "p" "P" "q" "Q" "S" "V"
manyDuplicates <- sample( 10:15, 10, replace = TRUE )
rank( manyDuplicates )
[1] 6.5 6.5 9.0 5.0 4.0 1.5 9.0 3.0 1.5 9.0
rank( manyDuplicates, ties.method = "min" )
[1] 6 6 8 5 4 1 8 3 1 8
rank( manyDuplicates, ties.method = "random" )
[1] 7 6 8 5 4 2 10 3 1 9
v <- c( -1, -0.5, 0, 0.5, 1, rnorm( 10 ) )
v
[1] -1.0000000 -0.5000000 0.0000000 0.5000000 1.0000000 -0.6146827 1.9218192 -0.4687374 -0.4697215 0.7662199
[11] -0.2776651 0.8835970 -0.9019558 -2.1718663 0.2127866
round( v, 0 )
[1] -1 0 0 0 1 -1 2 0 0 1 0 1 -1 -2 0
round( v, 1 )
[1] -1.0 -0.5 0.0 0.5 1.0 -0.6 1.9 -0.5 -0.5 0.8 -0.3 0.9 -0.9 -2.2 0.2
round( v, 2 )
[1] -1.00 -0.50 0.00 0.50 1.00 -0.61 1.92 -0.47 -0.47 0.77 -0.28 0.88 -0.90 -2.17 0.21
floor( v )
[1] -1 -1 0 0 1 -1 1 -1 -1 0 -1 0 -1 -3 0
ceiling( v )
[1] -1 0 0 1 1 0 2 0 0 1 0 1 0 -2 1
heights <- c( Amy = 166, Eve = 170, Bob = 177 )
heights
Amy Eve Bob
166 170 177
names( heights )
[1] "Amy" "Eve" "Bob"
names( heights ) <- c( "AMY", "EVE", "BOB" )
heights
AMY EVE BOB
166 170 177
heights[[ "EVE" ]]
[1] 170
expand_grid( x = c( 1:3, NA ), y = c( "a", "b" ) )
# A tibble: 8 x 2
x y
<int> <chr>
1 1 a
2 1 b
3 2 a
4 2 b
5 3 a
6 3 b
7 NA a
8 NA b
combn( c( "a", "b", "c", "d", "e" ), m = 2, simplify = TRUE )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d"
[2,] "b" "c" "d" "e" "c" "d" "e" "d" "e" "e"
combn( c( "a", "b", "c", "d", "e" ), m = 3, simplify = TRUE )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c"
[2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d"
[3,] "c" "d" "e" "d" "e" "e" "d" "e" "e" "e"
Copyright © 2021 Biomedical Data Sciences (BDS) | LUMC